<?php
namespace Home\Controller;
/**
 * Created by PhpStorm.
 * User: yhg97p
 * Date: 2016/4/6
 * Time: 21:14
 */
class PayController extends \Think\Controller {
    /**
     * 支付回调接口
     */
    public function nineteenPay(){
        $data = array();
        $data['info'] = json_encode($_REQUEST);
        $data['create_time'] = date("Y-m-d H:i:s");
        $data['ip'] = $_SERVER['REMOTE_ADDR'];
        $callback = M("CallbackParam")->add($data);
        if($_REQUEST['json']){
            $json = $_REQUEST['json'];
            $orderSno = json_decode($json,true)['out_trade_no'];
        }else{
            $orderSno = $_REQUEST['out_trade_no'];
        }
        $Invest = M("Invest");
        $InvestInfo = $Invest->where(array("order_no"=>$orderSno))->find();
        if($InvestInfo ){
            if($_REQUEST['trade_status'] && $_REQUEST['trade_status']!="TRADE_SUCCESS"){
                return;
            }
            $good_list = json_decode($InvestInfo['good_info'], true);
            if(count($good_list)>1){
                unset($InvestInfo['order_no']);
                unset($InvestInfo['id']);
                $user_id = $InvestInfo['user_id'];
                $newinvestinfo = $InvestInfo;
                foreach ($good_list as $key => $val) {
                    $good_info = array();
                    $gooddata = M("Good")->where(array("id"=>$val['good_id']))->find();
                    $good_info[0]["good_id"] = $val['good_id'];
                    $good_info[0]["count"] = $val['count'];
                    $good_info[0]["price"] = $val['price'];
                    $good_info[0]["sku"] = $val['sku'];
                    $amount = floatval($val['price']) * $val['count'];
                    $newinvestinfo['good_info'] = json_encode($good_info);
                    $newinvestinfo['paysuccess'] = '1';
                    $newinvestinfo['status'] = '3';
                    $newinvestinfo['postage'] = $gooddata['postage'];
                    $newinvestinfo['amount'] = floatval($amount) + floatval($newinvestinfo['postage']);
                    $res = D('Invest')->addOrder($newinvestinfo, $user_id);
                }
                $Invest->where(array('order_no' => $orderSno))->delete();
            }else{
                $Invest->where(array('order_no' => $orderSno))->save(array("paysucess"=>'1',"status"=>"3"));
            }
        }else{
            $Invest->where(array('order_no' => $orderSno))->save(array('paysuccess'=>'2'));
        }
        $this->ajaxReturn(array('error' => 0, 'info' => "执行成功"));
    }
    /**
     * 微信支付回调接口
     */
    public function wxPay(){
        $data = array();
        $data['info'] = json_encode($_REQUEST);
        $data['create_time'] = date("Y-m-d H:i:s");
        $data['ip'] = $_SERVER['REMOTE_ADDR'];
        $callback = M("CallbackParam")->add($data);
        require_once(dirname(dirname(dirname(__FILE__))).'/Notify/wx/pay.php');
        $wechatpay = new wechatpay();
        $wechatpay->payback();
    }

    //微信支付
    public function wechatpayinterface(){
        $param = I('params');
        $userId =  $this->user['userId'];
        $orderno = $param['order_id'];
        $userinfo = M("User")->where(array("id"=>$userId))->find();

        $investinfo = M("Invest")->where(array("order_no"=>$orderno))->find();

        $attributes = [
            'openid' => $userinfo['wechat'],
            'trade_type'       => 'APP', // JSAPI，NATIVE，APP...
            'body'             => "抢购惠".$orderno."批次商品",
            'detail'           => "抢购惠".$orderno."批次商品",
            'out_trade_no'     => $orderno,
            'spbill_create_ip'=> $_SERVER['REMOTE_ADDR'],
            'total_fee'        => 1,    //付款金额
        ];
        require_once(dirname(dirname(dirname(__FILE__))).'/Notify/wx/pay.php');
        $wechatpay = new wechatpay();
        $returninfo = json_decode($wechatpay->wepay($attributes),true);
        $this->ajaxReturn(array('error' => 0,'info'=>$returninfo));

    }

}